
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.housekeeper.controller.convert;

import com.huawei.housekeeper.common.model.ServiceDetail;
import com.huawei.housekeeper.commonutils.entity.OrderMessageEntity;
import com.huawei.housekeeper.controller.response.GetOrderDetailsVo;
import com.huawei.housekeeper.controller.response.GetOrdersOfCustomerVo;
import com.huawei.housekeeper.controller.response.GetOrdersOfTenantVo;
import com.huawei.housekeeper.dao.entities.Order;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;

/**
 * mapstruct转换器
 *
 * @author lWX1128557
 * @since 2022-02-23
 */
@Mapper(componentModel = "spring") // 启用映射转换
public interface OrderConvert {
    /**
     * 对象映射转换实例
     */
    OrderConvert INSTANCE = Mappers.getMapper(OrderConvert.class);

    /**
     * 订单映射客户订单列表Vo
     *
     * @param orders 订单表
     * @return 用户订单列表Vo
     */
    List<GetOrdersOfCustomerVo> toGetOrdersOfCustomerVo(List<Order> orders);

    /**
     * 订单表映射客户订单列表Vo
     *
     * @param order 订单表
     * @return 用户订单列表Vo
     */
    default GetOrdersOfCustomerVo mapCustomerVo(Order order) {
        GetOrdersOfCustomerVo ordersOfCustomerVo = new GetOrdersOfCustomerVo();

        // 订单详情封装
        String serviceDetailString = order.getServiceDetail();
        ServiceDetail serviceDetail = JSONObject.parseObject(serviceDetailString, ServiceDetail.class);
        ordersOfCustomerVo.setOrderId(order.getId());
        ordersOfCustomerVo.setEmployeeName(order.getEmployeeName());
        GetOrdersOfCustomerVo.ServiceDetail serviceDetailVo = new GetOrdersOfCustomerVo.ServiceDetail();
        serviceDetailVo.setServiceName(serviceDetail.getServiceDesc().getServiceName());
        GetOrdersOfCustomerVo.Specifications specification = new GetOrdersOfCustomerVo.Specifications();
        ArrayList<GetOrdersOfCustomerVo.Specifications> specifications = Lists.newArrayList();
        List<ServiceDetail.Selections> selections = serviceDetail.getServiceDesc().getSelections();
        selections.forEach(s -> {
            specification.setSpecName(s.getSpecName());
            specification.setOptionName(s.getOptionName());
            specifications.add(specification);
        });
        serviceDetailVo.setSpecifications(specifications);
        ordersOfCustomerVo.setServiceDetail(serviceDetailVo);
        ordersOfCustomerVo.setStatus(order.getStatus());
        ordersOfCustomerVo.setCreateTime(order.getCreatedTime());
        ordersOfCustomerVo.setOrderNumber(order.getOrderNumber());
        ordersOfCustomerVo.setAppointmentTime(order.getAppointmentTime());
        ordersOfCustomerVo.setAmount(order.getAmount());
        ordersOfCustomerVo.setPayment(order.getPayment());
        ordersOfCustomerVo.setImgSrc(serviceDetail.getServiceDesc().getImgSrc());
        return ordersOfCustomerVo;
    }

    /**
     * 订单转租户订单列表Vo
     *
     * @param orders 订单表
     * @return 租户订单列表Vo
     */

    List<GetOrdersOfTenantVo> toGetOrdersOfTenantVo(List<Order> orders);

    /**
     * 订单表映射租户订单列表Vo
     *
     * @param order 订单表
     * @return 租户订单列表Vo
     */
    default GetOrdersOfTenantVo mapTenantVo(Order order) {
        GetOrdersOfTenantVo ordersOfTenantVo = new GetOrdersOfTenantVo();
        ordersOfTenantVo.setOrderId(order.getId());
        ordersOfTenantVo.setOrderNumber(order.getOrderNumber());

        // 订单详情封装
        String serviceDetailString = order.getServiceDetail();
        ServiceDetail serviceDetail = JSONObject.parseObject(serviceDetailString, ServiceDetail.class);
        GetOrdersOfTenantVo.ServiceDetail serviceDetailVo = new GetOrdersOfTenantVo.ServiceDetail();
        serviceDetailVo.setServiceName(serviceDetail.getServiceDesc().getServiceName());
        serviceDetailVo.setPrice(serviceDetail.getServiceDesc().getPrice());
        ArrayList<GetOrdersOfTenantVo.Specifications> specificationsList = Lists.newArrayList();
        GetOrdersOfTenantVo.Specifications specifications = new GetOrdersOfTenantVo.Specifications();
        List<ServiceDetail.Selections> selections = serviceDetail.getServiceDesc().getSelections();
        selections.forEach(s -> {
            specifications.setSpecName(s.getSpecName());
            specifications.setOptionName(s.getOptionName());
            specificationsList.add(specifications);
        });
        serviceDetailVo.setSpecifications(specificationsList);
        ordersOfTenantVo.setServiceDetail(serviceDetailVo);
        ordersOfTenantVo.setPayment(order.getPayment());
        ordersOfTenantVo.setCustomerName(order.getCustomerName());
        ordersOfTenantVo.setServerName(order.getEmployeeName());
        ordersOfTenantVo.setAppointmentTime(order.getAppointmentTime());
        ordersOfTenantVo.setStatus(order.getStatus());
        ordersOfTenantVo.setImgSrc(serviceDetail.getServiceDesc().getImgSrc());
        return ordersOfTenantVo;
    }

    /**
     * 订单表映射订单详情表Vo
     *
     * @param order 订单表
     * @return 订单详情表Vo
     */
    default GetOrderDetailsVo mapOrderDetailsVo(Order order) {
        GetOrderDetailsVo orderDetailsVo = new GetOrderDetailsVo();

        // 订单详情封装
        String serviceDetailString = order.getServiceDetail();
        ServiceDetail serviceDetail = JSONObject.parseObject(serviceDetailString, ServiceDetail.class);
        GetOrderDetailsVo.ServiceDetail serviceDetailVo = new GetOrderDetailsVo.ServiceDetail();
        serviceDetailVo.setServiceName(serviceDetail.getServiceDesc().getServiceName());
        GetOrderDetailsVo.Specifications specification = new GetOrderDetailsVo.Specifications();
        ArrayList<GetOrderDetailsVo.Specifications> specifications = Lists.newArrayList();
        List<ServiceDetail.Selections> selections = serviceDetail.getServiceDesc().getSelections();
        selections.forEach(s -> {
            specification.setSpecName(s.getSpecName());
            specification.setOptionName(s.getOptionName());
            specifications.add(specification);
        });
        serviceDetailVo.setSpecifications(specifications);
        orderDetailsVo.setServiceDetail(serviceDetailVo);
        if (4 == order.getStatus()) {
            orderDetailsVo.setOrderTime(order.getUpdatedTime());
        } else if (2 == order.getStatus()) {
            orderDetailsVo.setCompletedTime(order.getUpdatedTime());
        }
        orderDetailsVo.setOrderId(order.getId());
        orderDetailsVo.setOrderNumber(order.getOrderNumber());
        orderDetailsVo.setCreateTime(order.getCreatedTime());
        orderDetailsVo.setAmount(order.getAmount());
        orderDetailsVo.setPayment(order.getPayment());
        orderDetailsVo.setStatus(order.getStatus());
        orderDetailsVo.setCustomerName(order.getCustomerName());
        orderDetailsVo.setCustomerPhone(order.getCustomerPhone());
        orderDetailsVo.setAppointmentTime(order.getAppointmentTime());
        orderDetailsVo.setAddress(order.getAddress());
        orderDetailsVo.setServerName(order.getEmployeeName());
        orderDetailsVo.setServerPhone(order.getEmployeePhone());
        orderDetailsVo.setImgSrc(serviceDetail.getServiceDesc().getImgSrc());
        orderDetailsVo.setRemark(order.getRemark());
        return orderDetailsVo;
    }

    /**
     * 订单表映射订单消息entity
     *
     * @param order 订单表
     * @return 订单消息体
     */
    default OrderMessageEntity mapOrderMessageEntity(Order order) {
        OrderMessageEntity orderMessageEntity = new OrderMessageEntity();
        orderMessageEntity.setRemark(order.getRemark());
        orderMessageEntity.setCustomerPhone(order.getCustomerPhone());
        orderMessageEntity.setAddress(order.getAddress());
        orderMessageEntity.setCustomerName(order.getCustomerName());
        orderMessageEntity.setAmount(order.getAmount());
        orderMessageEntity.setAppointment(order.getAppointmentTime());
        orderMessageEntity.setCustomerId(order.getCustomerId());
        orderMessageEntity.setOrderId(order.getId());
        orderMessageEntity.setPayment(order.getPayment());

        // 订单详情封装
        String serviceDetailString = order.getServiceDetail();
        ServiceDetail serviceDetail = JSONObject.parseObject(serviceDetailString, ServiceDetail.class);
        OrderMessageEntity.Selections selection = new OrderMessageEntity.Selections();
        ArrayList<OrderMessageEntity.Selections> selectionslist = Lists.newArrayList();
        List<ServiceDetail.Selections> selections = serviceDetail.getServiceDesc().getSelections();
        selections.forEach(s -> {
            selection.setSpecName(s.getSpecName());
            selection.setSelection(s.getOptionName());
            selectionslist.add(selection);
        });
        orderMessageEntity.setServiceName(serviceDetail.getServiceDesc().getServiceName());
        OrderMessageEntity.ServiceDetail detail = new OrderMessageEntity.ServiceDetail();
        detail.setSelections(selectionslist);
        orderMessageEntity.setServiceDetail(detail);
        return orderMessageEntity;
    }
}